package code.jam.activity;

import code.jam.R;
import code.jam.core.JamManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class JamActivitySelectOctave extends Activity 
{
	public static final String DEBUG_TAG = "JamActivitySetOctave";
	private view v = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.v = new view(this);
		setContentView(this.v);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}
	
	@Override
	protected void onRestart() 
	{
		Log.d(DEBUG_TAG, "OnRestart()");
		finish();
		super.onRestart();
	}
	
	@Override
	protected void onDestroy() 
	{
		Log.d(DEBUG_TAG, "OnDestroy()");
		finish();
		super.onDestroy();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) 
	{
		float x = event.getX();
		float y = event.getY();
		
		Log.d(DEBUG_TAG, String.format("x : %f = y : %f", event.getX(), event.getY()));
		
		boolean easytoplay =  ((x > 0)&&(x < 240)); 
		
		boolean hardtoplay = ((x > 240)&&(x < 480)); 
		
		if(event.getAction() == MotionEvent.ACTION_DOWN)
		{
			if(easytoplay)
			{
				v.selectEasyToPlay();
			}
			
			else if(hardtoplay)
			{
				v.selectHardToPlay();
			}
		}
		
		if(event.getAction() == MotionEvent.ACTION_UP)
		{
			if(easytoplay)
			{
				JamManager.getInstance().getInstrumentManager().setOctave( 1 );
				Intent i = new Intent();
				i.setClass(this, code.jam.activity.JamActivityPlayMusic.class);
				startActivity(i);
			}
			else if(hardtoplay)
			{
				JamManager.getInstance().getInstrumentManager().setOctave( 3 );
				Intent i = new Intent();
				i.setClass( this, code.jam.activity.JamActivityPlayMusic.class);
				startActivity( i);
			}
		}

		return super.onTouchEvent(event);
	}

	class view extends ImageView 
	{
		Context context = null;

		public view(Context context) 
		{
			super(context);
			setImageResource(R.drawable.select_octave_none);
		}
		public void selectHardToPlay()
		{
			setImageResource(R.drawable.select_octave_hard);
		}
		public void selectEasyToPlay() 
		{
			setImageResource(R.drawable.select_octave_easy);
		}

		public void selectNone() 
		{
			setImageResource(R.drawable.select_octave_none);
		}
	}
}
